package base64;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.Writer;
import java.nio.charset.Charset;

public class Base64Writer extends Writer{

  private final static byte[] TO_BYTES = { -1, -1, -1, -1, -1, -1, -1, -1, -1,
      -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
      -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 62, -1,
      -1, -1, 63, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, -1, -1, -1,
      -1, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18,
      19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1, -1, 26, 27, 28, 29, 30,
      31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48,
      49, 50, 51};
  
  public static void main(String[] args) throws IOException{
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    Base64Writer writer = new Base64Writer(baos);
    writer.write("RnJhbnogamFndCBpbSBrb21wbGV0dCB2ZXJ3YWhybG9zdGVuIFRheGkgcXVlciBkdXJjaCBCYXllcm4=");
    writer.close();
    String result = new String(baos.toByteArray(), Charset.forName("ascii"));
    System.out.println(result);
  }
  
  private OutputStream out;
  
  public static byte[] toBytes(String base64) throws IOException{
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    Base64Writer writer = new Base64Writer(out);
    writer.write(base64);
    writer.close();
    return out.toByteArray();
  }

  public Base64Writer(OutputStream out){
    this.out = out;
  }
  
  @Override
  public void close() throws IOException {
    out.close();
    if (state != 0)
      throw new IOException("invalid end of base64 stream");
  }

  @Override
  public void flush() throws IOException {
    out.flush();
  }

  @Override
  public void write(char[] cbuf, int off, int len) throws IOException {
    int pos = off;
    for (int i = 0; i < len; i++){
      writeChar(cbuf[pos]);
      pos++;
    }
  }
  
  private byte state = 0;
  
  private byte memory;
  
  private boolean endReached = false;
  
  public void writeChar(char c) throws IOException {
    if (c == '='){
      endReached = true;
      if (state == 3)
        state = 0;
      else
        state++;
      return;
    }
    if (endReached)
      throw new IOException("Invalid base64 character behind =");
    
    
    
    byte value;
    try{
      value = TO_BYTES[c];
    } catch (ArrayIndexOutOfBoundsException aioobe){
      throw new IOException("Invalid base64 character '" +c + "'");
    }
    if (value == -1)
      throw new IOException("Invalid base64 character '" +c + "'");
    
    switch (state) {
    case 0:
      state = 1;
      memory = (byte)(value << 2);
      break;
    case 1:
      out.write(memory | (value >> 4));
      memory = (byte)(value << 4);
      state = 2;
      break;
    case 2:
      out.write(memory | (value >> 2));
      memory = (byte)(value << 6);
      state = 3;
      break;
    case 3:
      out.write(memory | (value &0x3f));
      state = 0;
      break;
    }
  }

}
